package com.zy.openchat.core.model.message;

import android.os.Parcel;
import android.os.Parcelable;

import com.zy.openchat.core.manager.ChatExCacheManager;
import com.zy.openchat.core.model.group.GroupSendInfo;
import com.zy.openchat.core.model.group.SuperGroupInfo;
import com.zy.openchat.core.model.photo.ProfilePhoto;
import com.zy.openchat.core.model.user.AbsGroupInfo;
import com.zy.openchat.core.model.user.UserInfo;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;

public class ChatInfo implements Parcelable, AbsGroupInfo {
    public long id;
    public ChatType type;
    //标题-群组时使用，单聊使用User中displayName()
    public String title;
    //title对应的拼音
    public String title_full_py;
    //头像-群组使用，单聊使用User中的头像
    public ProfilePhoto photo;
    //权限
    public ChatPermissions permissions;
    public boolean is_marked_as_unread;
    //未读数量
    public int unread_count;
    public int unread_mention_count;
    public long last_read_inbox_message_id;
    //消息已读最大值
    public long last_read_outbox_message_id;

    public ArrayList<Long> group_uers = new ArrayList<>();

    //会话最后一条消息
    public MessageInfo lastMessage;

    //是否置顶
    public boolean is_pinned;
    //是否提醒
    public boolean default_disable_notification;
    public int online_member_count;
    public long order;

    @Override
    public ProfilePhoto photo() {
        return photo;
    }

    @Override
    public String title() {
        return title;
    }

    @Override
    public long chatId() {
        return id;
    }

    public String getGroupTitle(SuperGroupInfo groupInfo) {
        if (!isGroup()) {
            return "";
        }
//        if(groupInfo==null){
//            return title;
//        }
        int count = ChatExCacheManager.getChatExCacheManager().getGroupMemberCount(id, groupInfo);
        if (count > 0) {
            return String.format("%s(%d)", title+title, count);
        } else {
            return title;
        }
    }

    public String getGroupCount(SuperGroupInfo groupInfo) {
        if (!isGroup()) {
            return "";
        }
        int count = ChatExCacheManager.getChatExCacheManager().getGroupMemberCount(id, groupInfo);
        if (count > 0) {
            return String.format("(%d)",  count);
        } else {
            return "";
        }
    }

    //排序依据
    public long modifyDate() {
        if (lastMessage != null)
            return lastMessage.date;
        return 0;
    }

    //拷贝除lastMessage之外的所有值
    public void copyChatContent(ChatInfo info) {
        if (info != null) {
            this.type = info.type;
            this.title = info.title;
            this.photo = info.photo;
            this.permissions = info.permissions;
            this.is_marked_as_unread = info.is_marked_as_unread;
            this.unread_count = info.unread_count;
            this.unread_mention_count = info.unread_mention_count;
            this.last_read_inbox_message_id = info.last_read_inbox_message_id;
            this.last_read_outbox_message_id = info.last_read_outbox_message_id;
            this.is_pinned = info.is_pinned;
            this.default_disable_notification = info.default_disable_notification;
        }
    }

    //关键字是否匹配
    public boolean isMatch(String keyword) {
        if (!StringUtils.isEmpty(keyword)) {
            if (!StringUtils.isEmpty(title)) {
                if (title.toUpperCase().contains(keyword.toUpperCase())) {
                    return true;
                }
            }
            if (!StringUtils.isEmpty(title_full_py)) {
                if (title_full_py.toUpperCase().contains(keyword.toUpperCase())) {
                    return true;
                }
            }
        }
        return false;
    }
    /**
     * 是否群组
     *
     * @return true 表示普通群组或者超级群组
     */
    public boolean isGroup() {
        if (type != null) {
            return "chatTypeBasicGroup".equals(type.type) || "chatTypeSupergroup".equals(type.type);
        }
        return false;
    }

    public boolean isSuperGroup() {
        if (type != null) {
            return "chatTypeSupergroup".equals(type.type);
        }
        return false;
    }

    public boolean isSecretChat() {
        if (type != null) {
            return "chatTypeSecret".equals(type.type);
        }
        return false;
    }

    public boolean isPrivateChat() {
        if (type != null) {
            return "chatTypePrivate".equals(type.type);
        }
        return false;
    }

    public boolean isGroupSendChat() {
        if (type != null) {
            return GroupSendInfo.CHAT_TYPE_FOR_GROUP_SEND.equals(type.type);
        }
        return false;
    }

    public long secretChatId(){
        return type==null?0:type.secret_chat_id;
    }
    /**
     * 是否我的收藏
     *
     * @return
     */
    public boolean isMyFovChat() {
        return !isGroup() && id == UserInfo.getUserInfo().id;
    }

    public long userId() {
        if (type != null) {
            return type.user_id;
        }
        return 0;
    }

    public long groupId() {
        if (type != null) {
            return type.basic_group_id;
        }
        return 0;
    }

    public long superGroupId() {
        if (type != null) {
            return type.supergroup_id;
        }
        return 0;
    }

    public static long toServerPeerId(long localChatId) {
        String str = "" + localChatId;
        if (str.startsWith("-100")) {
            str = str.substring("-100".length() - 1);
            return Long.parseLong(str);
        }
        return localChatId;
    }

    public static long toLocalChatId(long serverPeerId) {
        String str = "" + serverPeerId;
        if (!str.startsWith("-100")) {
            str = "-100" + str;
            return Long.parseLong(str);
        }
        return serverPeerId;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(this.id);
        dest.writeParcelable(this.type, flags);
        dest.writeString(this.title);
        dest.writeString(this.title_full_py);
        dest.writeParcelable(this.photo, flags);
        dest.writeParcelable(this.permissions, flags);
        dest.writeByte(this.is_marked_as_unread ? (byte) 1 : (byte) 0);
        dest.writeInt(this.unread_count);
        dest.writeInt(this.unread_mention_count);
        dest.writeLong(this.last_read_inbox_message_id);
        dest.writeLong(this.last_read_outbox_message_id);
        dest.writeParcelable(this.lastMessage, flags);
        dest.writeByte(this.is_pinned ? (byte) 1 : (byte) 0);
        dest.writeByte(this.default_disable_notification ? (byte) 1 : (byte) 0);
    }

    public ChatInfo() {

    }

    protected ChatInfo(Parcel in) {
        this.id = in.readLong();
        this.type = in.readParcelable(ChatType.class.getClassLoader());
        this.title = in.readString();
        this.title_full_py = in.readString();
        this.photo = in.readParcelable(ProfilePhoto.class.getClassLoader());
        this.permissions = in.readParcelable(ChatPermissions.class.getClassLoader());
        this.is_marked_as_unread = in.readByte() != 0;
        this.unread_count = in.readInt();
        this.unread_mention_count = in.readInt();
        this.last_read_inbox_message_id = in.readLong();
        this.last_read_outbox_message_id = in.readLong();
        this.lastMessage = in.readParcelable(MessageInfo.class.getClassLoader());
        this.is_pinned = in.readByte() != 0;
        this.default_disable_notification = in.readByte() != 0;
    }

    public static final Creator<ChatInfo> CREATOR = new Creator<ChatInfo>() {
        @Override
        public ChatInfo createFromParcel(Parcel source) {
            return new ChatInfo(source);
        }

        @Override
        public ChatInfo[] newArray(int size) {
            return new ChatInfo[size];
        }
    };

    public boolean isMessageEqual(ChatInfo chatInfo){
        if(chatInfo==null){
            return false;
        }
        return chatInfo.id == id;
    }

    public boolean isAvaterDownloaded = false;

    public boolean isAvaterDownloadEqual(ChatInfo fullInfo){
        if(fullInfo==null){
            return false;
        }
        UserInfo oldUserInfo = UserInfo.getUserInfo(type.user_id);
        UserInfo newUserInfo = UserInfo.getUserInfo(fullInfo.type.user_id);
        if(oldUserInfo==null&&newUserInfo==null){
            return true;
        }
        if(oldUserInfo==null||newUserInfo==null){
            return false;
        }
        boolean newIsAvaterDownloaded = false;
        if(newUserInfo.profile_photo!=null&&newUserInfo.profile_photo.isSmallPhotoDownloaded()){
            newIsAvaterDownloaded = true;
        }
        return newIsAvaterDownloaded == isAvaterDownloaded;
    }

    public boolean isMine = false;
}
